package module.ahleteView;

import bean.Constant;
import util.UI.SwingUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @ Auther leiHua
 * @ Date 2022/5/8
 * 运动员界面
 */
public class AthleteView extends JFrame {
    //窗口宽度
    int jframeWidth=1280;
    //窗口高度
    int jframeHeight=1400;
    public static TrayIcon trayicon;// 系统托盘图标
    public AthleteView(){
        SwingUI.setUI();

        this.setTitle("广软运动会");
        this.setLayout(null);
        //设置窗口图标
        ImageIcon imageIcon = new ImageIcon("src/imag/logo.jpg");
        Image image = imageIcon.getImage();
        this.setIconImage(image);
        //创建轮播图
        String[] imgPath = {"src/imag/6.jpg","src/imag/2.jpg","src/imag/3.jpg","src/imag/4.jpg","src/imag/5.jpg","src/imag/1.jpg","src/imag/15.jpg"};
        AthleteImageJpanel imgJpanel = new AthleteImageJpanel(imgPath,1280,500);
        imgJpanel.setBounds(0,0,1280,500);
        JPanel labelPanel = new JPanel();
        JLabel jLabel = new JLabel("广软运动会系统");
        jLabel.setFont(new Font("宋体",Font.BOLD,60));
        labelPanel.add(jLabel);
        jLabel.setBounds(100,50,500,100);
        this.getLayeredPane().add(imgJpanel,new Integer(Integer.MIN_VALUE));
        this.getLayeredPane().add(jLabel,new Integer(Integer.MAX_VALUE));
        //创建中间菜单按钮
        AthleteButtonBar athleteButtonBar = new AthleteButtonBar();
        JPanel JPanelBar = athleteButtonBar.getjPanel();
        JPanelBar.setBounds(40,510,JPanelBar.getWidth(),JPanelBar.getHeight());

        //创建信息表
        this.add(labelPanel);
        this.add(imgJpanel);
        this.add(JPanelBar);
        this.setBounds((Constant.SCREEN_WIDETH - jframeWidth) / 2, (Constant.SCREEN_HEIGHT - jframeHeight) / 2, jframeWidth, jframeHeight);
        //this.setResizable(false); //大小不可变
        this.setVisible(true);

        //点击关闭，将系统隐藏到系统托盘中
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                systemTrayInitial();
            }
        });

    }

    /**
     * 创建系统托盘
     */
    private void systemTrayInitial() {
        if (!SystemTray.isSupported()) // 判断当前系统是否支持系统栏
            return;
        try {
            String title = "广软运动会系统";// 系统栏通知标题
            String personal = "雷诏华";// 系统通知栏内容
            SystemTray sysTray = SystemTray.getSystemTray();// 获取系统默认托盘
            Image image = Toolkit.getDefaultToolkit().getImage(AthleteView.class.getResource("/imag/logo.jpg"));// 创建系统栏图标
            trayicon = new TrayIcon(image, title + "\n" + personal, createMenu());//创建系统栏图标对象
            trayicon.setImageAutoSize(true);	//设置自动大小
            trayicon.addActionListener(new ActionListener() {   //最小化托盘的双击事件
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(true);
                    toFront();
                }
            });//添加事件监听器
            sysTray.add(trayicon);//添加系统栏图标到系统托盘
            trayicon.displayMessage(title, personal, TrayIcon.MessageType.INFO);	//显示系统栏图标提示文本
            this.setVisible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 创建系统托盘的最小化菜单栏
     * @return
     */
    private PopupMenu createMenu() {
        PopupMenu menu = new PopupMenu();
        MenuItem exitItem = new MenuItem("exit");
        exitItem.addActionListener(new ActionListener() { // 系统栏退出事件
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        MenuItem openItem = new MenuItem("open");
        openItem.addActionListener(new ActionListener() {// 系统栏打开菜单项事件
            public void actionPerformed(ActionEvent e) {
                if (!isVisible()) {	//如果主窗体隐藏
                    setVisible(true);//显示主窗体
                    toFront();	//使主窗体显示在最上层
                } else
                    toFront();	//使主窗体显示在最上层
            }
        });
        menu.add(openItem);//添加打开菜单项
        menu.addSeparator();//添加菜单项分隔符
        menu.add(exitItem);//添加退出菜单项

        return menu;
    }


    public static void main(String[] args) {

        AthleteView athleteView = new AthleteView();
        System.out.println(athleteView.getSize());
    }

}
